import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import HtmlFilter from "html-filter";
import { AsYouType, isValidPhoneNumber } from "libphonenumber-js";
import { config } from "@config";

const htmlFilter = new HtmlFilter();
htmlFilter.allowedTags = config.student.aboutMeAllowedTags;

const updateBody = Type.Object({
    sex: Type.Optional(Type.Number({ minimum: 1, maximum: 2 })),
    birthDate: Type.Optional(Type.String({ format: "date" })),
    metro: Type.Optional(Type.String()),
    city: Type.Optional(Type.String()),
    professionalRole: Type.Optional(Type.String()),
    skills: Type.Array(Type.String(), {
        maxItems: config.student.maxSkills,
    }),
    qualification: Type.String(),
    aboutMe: Type.String(),
    address: Type.String(),
    personalities16: Type.Optional(Type.String()),
    isLazy: Type.Boolean(),
    isAlcoholic: Type.Boolean(),
    isReadyRelocation: Type.Boolean(),
    isReadyRemote: Type.Boolean(),
    contacts: Type.Object({
        phones: Type.Array(Type.String()),
        emails: Type.Array(Type.String({ format: "email" })),
        vk: Type.String(),
        telegram: Type.String(),
        github: Type.String(),
    }),
    mainEdu: Type.Array(
        Type.Object({
            name: Type.String(),
            address: Type.String(),
            startDate: Type.String(),
            endDate: Type.String(),
        })
    ),
    extraEdu: Type.Array(
        Type.Object({
            name: Type.String(),
            address: Type.String(),
            startDate: Type.String(),
            endDate: Type.String(),
        })
    ),
});

export const update = async (fastify: FastifyInstance) => {
    fastify.post<{
        Body: Static<typeof updateBody>;
    }>(
        "/student/update",
        {
            preHandler: fastify.auth("student"),
            schema: {
                tags: ["student"],
                body: updateBody,
            },
        },
        async (req, res) => {
            const {
                professionalRole,
                skills,
                contacts,
                city,
                metro,
                aboutMe,
                sex,
                birthDate,
                qualification,
                mainEdu,
                extraEdu,
                address,
                personalities16,
                isLazy,
                isAlcoholic,
                isReadyRelocation,
                isReadyRemote,
            } = req.body;

            contacts.phones = contacts.phones
                .map((phone) => {
                    if (!isValidPhoneNumber(phone, "RU")) return;
                    return new AsYouType("RU").input(phone);
                })
                .filter((x) => x);

            console.log(contacts.phones);
            await req.student.update({
                city,
                metro,
                sex,
                birthDate,
                aboutMe: htmlFilter.filter(aboutMe),
                professionalRole,
                skills,
                contacts,
                qualification,
                mainEdu,
                extraEdu,
                address,
                personalities16,
                isLazy,
                isAlcoholic,
                isReadyRelocation,
                isReadyRemote,
            });

            return res.send({
                ...req.student.dataValues,
                photo: req.student.photo
                    ? "https://" +
                      config.host +
                      "/files/avatars/" +
                      req.student.photo
                    : null,
                password: undefined,
                updatedAt: undefined,
            });
        }
    );
};
